#!/bin/bash

NIC_NAME="enp6s0"

LOCAL_DIRECTORY=$(pwd)   
BASE_DIRECTORY=$(dirname "$(pwd)")

TOOL_DIRECTORY=$BASE_DIRECTORY/UMSD_v7.0.0/host/linux
PASSWD=ros #Please change this to your password.
COMMAND=$1

CONFIG_FILE=$TOOL_DIRECTORY/umsd.cfg

if [ -f "$CONFIG_FILE" ]; then
   CURRENT_NIC_NAME=$(grep -Po '(?<=nic_name\s).*' "$CONFIG_FILE")
   if [ "$CURRENT_NIC_NAME" = "$NIC_NAME" ];then
      :
   else
      sed -i "s/^[\t ]*nic_name.*/\t\tnic_name $NIC_NAME/" "$CONFIG_FILE"
      echo "NIC name in the configuration file has been updated"
   fi
else
   echo "Configuration file "umsd.cfg" not found."
   echo -e "\nPress Enter to continue..."
   read  
   exit 1
fi
shift

cd $TOOL_DIRECTORY
echo "$PASSWD" | sudo -S true

gnome-terminal -- bash -c "echo $PASSWD | sudo -S ./UMSD_MCLI -f $CONFIG_FILE"
#MCLI_PID=$(echo $PASSWD | sudo -S lsof -i :8888 | grep "LISTEN" | awk '{print $2}')
echo "$@"
sleep 1
cd - > /dev/null

case $COMMAND in
   "setPhy")
      expect umsd_mcli setPhyMode "$@"
      ;;
   "setVlan")
      expect umsd_mcli dqacVlan bind "$@"
      ;;
   "resetVlan")
      expect umsd_mcli dqacVlan reset
      ;;
   *)
      echo -e "\n\nInvalid command: $COMMAND"
      echo "Usage: $0 {setPhy|setVlan|resetVlan} arg1 arg2 ..."
      sudo sh -c 'lsof -ti :8888 | xargs sudo kill -9'
      echo -e "\nPress Enter to continue..."
      read  
      exit 1
      ;;
esac
   #sudo kill $MCLI_PID

#TELNET_PID=$(echo $PASSWD | sudo -S lsof -i :5202 | grep "telnet" | awk '{print $2}') 
sleep 2
sudo lsof -ti :8888 | xargs sudo kill -9

echo -e "\nPress Enter to continue..."
read
exit 0
